<template>
  <div class="goods-box2">
    <div class="search">
      <div>
        <el-input v-model="searchValue" maxlength="100" clearable style="width:290px;" />
        <img src="../assets/img/search.png" class="search-icon" @click="queryGoods" />
      </div>
      <div style="color:#696969;font-size:12px;margin-top: 10px;">
        热门搜索：
        <a class="tag-item">苹果</a>
        <a class="tag-item">百香果</a>
        <a class="tag-item">榴莲</a>
      </div>
    </div>
    <div class="goods" v-for="(item, index) in goods" :key="index" @click="detailsClick(item.orderId)">
      <img v-if="item.picture" :src="'https://rongxiaotong.oss-cn-beijing.aliyuncs.com/order/' + item.picture" class="goods-img" />
      <img v-else src="https://rongxiaotong.oss-cn-beijing.aliyuncs.com/order/wutu.gif" class="goods-img" />
      <div class="info">
        <p class="content">
          <i style="color:#696969;font-size:12px;font-style:normal;" v-if="item.type == 'goods'">[供]</i>
          <i style="color:#696969;font-size:12px;font-style:normal;" v-else>[需]</i>
          <span class="content-text">{{ item.content }}</span>
        </p>
        <span class="price" v-if="item.price"><i style="font-size:5px">￥</i>{{ item.price }}</span>
        <span class="initiator">{{ item.ownName }} | {{ formatDateTime(item.createTime) }}</span>
      </div>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { useRouter } from 'vue-router';

const searchValue = ref('')

const { goods } = defineProps({
  goods: {
    type: Object, // 接收的参数类型
    default: {
      orderId: '',
      picture: '',
      content: '',
      price: '',
      ownName: '',
      createTime: '',
      type: '',
    }
  },
})

const router = useRouter()

function detailsClick(orderId: string) {
  router.push({
    name: 'details',
    params: {
      orderId: orderId
    }
  });
}

function formatDateTime(createTime: string) {
  const date = new Date(createTime);
  return date.toLocaleString();
}

const emit = defineEmits(['queryGoods',])


const queryGoods = () => {
  emit('queryGoods', searchValue.value)
}

</script>

<style lang="less" scoped>
.search {
  width: 624px;
  height: 80px;
  background-color: white;
  padding: 10px 20px;

  .tag-item {
    margin-right: 10px;
    cursor: pointer;
  }

  .search-icon {
    position: relative;
    bottom: -13px;
    left: -2px;
    cursor: pointer;
  }
}

.goods-box2 {
  background-color: #fff;
  width: 780px;
  margin: 0 auto;

  .goods {
    width: 780px;
    min-height: 120px;
    padding: 10px 20px;
    display: flex;
    flex-direction: row;
    justify-content: flex-start;
    background-color: white;
    border-radius: 0px;
    border-bottom: 1px solid #f2f2f2;
    text-align: left;

    .goods-img {
      width: 100px;
      height: 100px;
      margin-right: 20px;
      border-radius: 6px;
    }

    .info {
      width: 600px;

      .initiator {
        color: #666;
      }

      .content {
        width: 580px;
        line-height: 20px;
        /*超出的部分隐藏*/
        overflow: hidden;
        /*文字用省略号替代超出的部分*/
        text-overflow: ellipsis;
        /*弹性伸缩盒子模型显示*/
        display: -webkit-box;
        /*限制在一个块元素显示文本的行数*/
        -webkit-line-clamp: 7;
        /*设置或检索伸缩盒对象的子元素排列方式*/
        -webkit-box-orient: vertical;
        font-size: 15px;
        display: flex;
        flex-direction: row;
        justify-content: flex-start;

        .content-text {
          cursor: pointer;

          &:hover {
            color: #035D1C;
            text-decoration: underline;
          }
        }
      }

      .price {
        font-size: 15px;
        font-weight: bold;
        display: block;
        color: red;
      }
    }
  }
}

.search /deep/ .el-input--suffix .el-input__inner {
  height: 35px;
  line-height: 35px;
}
</style>